﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace itChatAdmin
{
    public partial class FormMsgAdmin : Form
    {
        public FormMsgAdmin()
        {
            InitializeComponent();
        }
        String UserID = "";
        private void button26_Click(object sender, EventArgs e)
        {
            RefreshMessageList();
        }
        private void RefreshMessageList()
        {
            this.listView5.Items.Clear();

            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);

            Boolean MyClass = false;
            Boolean MyMsg = false;

            if (this.comboBox1.Text.Trim() != "")
            {
                MyClass = true;
            }

            if (this.textBox11.Text.Trim() != "")
            {
                MyMsg = true;
            }

            if (MyClass == true && MyMsg == true)
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages WHERE MsgClass=@MsgClass AND Messages like '%@Messages'%";
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Messages"));
                mySQLiteCommand.Parameters["@MsgClass"].Value = this.comboBox1.Text.Trim();
                mySQLiteCommand.Parameters["@Messages"].Value = this.textBox11.Text.Trim();
            }
            else if (MyClass == true)
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages WHERE MsgClass=@MsgClass";
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                mySQLiteCommand.Parameters["@MsgClass"].Value = this.comboBox1.Text.Trim();
            }
            else if (MyMsg == true)
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages WHERE Messages=@Messages";
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Messages"));
                mySQLiteCommand.Parameters["@Messages"].Value = this.textBox11.Text.Trim();
            }
            else
            {
                mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages";
            }

            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            mySQLiteCommand.Parameters.Clear();

            while (mySQLiteDataReader.Read())
            {
                String ID = mySQLiteDataReader["ID"].ToString().Trim();
                String MsgClass = mySQLiteDataReader["MsgClass"].ToString().Trim();
                String Messages = mySQLiteDataReader["Messages"].ToString().Trim();
                Boolean IsEnabled = Boolean.Parse(mySQLiteDataReader["IsEnabled"].ToString().Trim());

                ListViewItem item = new ListViewItem(new string[] { ID, MsgClass, Messages }, 0);
                if (IsEnabled != true)
                {
                    item.BackColor = Color.DarkGray;
                }
                this.Invoke((EventHandler)delegate { listView5.Items.Add(item); });
            }
            mySQLiteCommand.Parameters.Clear();
            mySQLiteDataReader.Close();


            this.comboBox1.Items.Clear();
            this.comboBox2.Items.Clear();
            mySQLiteCommand.CommandText = "SELECT * FROM MelaMessagesClass ORDER BY ClassName";
            SQLiteDataReader mySQLiteDataReader1 = mySQLiteCommand.ExecuteReader();
            while (mySQLiteDataReader1.Read())
            {
                this.comboBox1.Items.Add(mySQLiteDataReader1["ClassName"].ToString().Trim());
                this.comboBox2.Items.Add(mySQLiteDataReader1["ClassName"].ToString().Trim());
            }
            mySQLiteCommand.Parameters.Clear();
            mySQLiteDataReader1.Close();

        }

        private void button25_Click(object sender, EventArgs e)
        {
            //添加消息分类
            if (this.comboBox2.Text.Trim() == "")
            {
                return;
            }

            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaMessagesClass WHERE ClassName=@ClassName";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ClassName"));
            mySQLiteCommand.Parameters["@ClassName"].Value = this.comboBox2.Text.Trim();
            Object i = mySQLiteCommand.ExecuteScalar();
            if (int.Parse(i.ToString()) > 0)
            {
                MessageBox.Show("重复的分类！");
                return;
            }
            mySQLiteCommand.Parameters.Clear();


            mySQLiteCommand.CommandText = "INSERT INTO MelaMessagesClass(ClassName) VALUES(@ClassName)";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ClassName"));
            mySQLiteCommand.Parameters["@ClassName"].Value = this.comboBox2.Text.Trim();
            mySQLiteCommand.ExecuteNonQuery();
            Form1.WriteToDBInDisk(mySQLiteCommand);
            mySQLiteCommand.Parameters.Clear();
            //RefreshMsgClass();
            MessageBox.Show("新分类[" + this.comboBox2.Text.Trim() + "]添加成功！");
            this.comboBox2.Text = "";
        }

        private void button24_Click(object sender, EventArgs e)
        {
            //下载数据
            String SqlServerString = AppConfig.GetAppSettingValue("SqlServerString");
            if (SqlServerString == "")
            {
                SqlServerString = "Data Source=202.115.129.218;Initial Catalog=itChat;User Id=zqm;Password=mingming1;";
                AppConfig.SetAppSettingKeyValue("SqlServerString", SqlServerString);
            }
            SqlConnection MySqlConnection = new SqlConnection(SqlServerString);
            SqlCommand MySqlCommand = new SqlCommand("", MySqlConnection);
            try
            {
                MySqlConnection.Open();
                MySqlCommand.CommandText = @"SELECT * FROM [dbo].[MaleMarketing] where IsEnable=1";
                SqlDataReader MyReader = MySqlCommand.ExecuteReader();
                SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
                while (MyReader.Read())
                {
                    String Msg = MyReader["Msg"].ToString().Trim();
                    String MsgClass = MyReader["MsgClass"].ToString().Trim();

                    mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaMessages WHERE MsgClass=@MsgClass AND Messages=@Messages";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Messages"));
                    mySQLiteCommand.Parameters["@MsgClass"].Value = MsgClass;
                    mySQLiteCommand.Parameters["@Messages"].Value = Msg;
                    object i = mySQLiteCommand.ExecuteScalar();
                    mySQLiteCommand.Parameters.Clear();

                    if (int.Parse(i.ToString().Trim()) > 0) continue;

                    mySQLiteCommand.CommandText = "INSERT INTO MelaMessages(MsgClass,Messages) VALUES(@MsgClass,@Messages)";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Messages"));
                    mySQLiteCommand.Parameters["@MsgClass"].Value = MsgClass;
                    mySQLiteCommand.Parameters["@Messages"].Value = Msg;
                    mySQLiteCommand.ExecuteNonQuery();
                    Form1.WriteToDBInDisk(mySQLiteCommand);
                    mySQLiteCommand.Parameters.Clear();
                }

                //更新分类
                ArrayList ClassList = new ArrayList();
                mySQLiteCommand.CommandText = "SELECT DISTINCT MsgClass FROM MelaMessages";
                SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
                while (mySQLiteDataReader.Read())
                {
                    ClassList.Add(mySQLiteDataReader["MsgClass"].ToString().Trim());
                }
                mySQLiteDataReader.Close();
                mySQLiteCommand.Parameters.Clear();

                foreach (string f in ClassList)
                {
                    if (f.Trim() == "") continue;
                    mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaMessagesClass WHERE ClassName=@MsgClass";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                    mySQLiteCommand.Parameters["@MsgClass"].Value = f;

                    object i = mySQLiteCommand.ExecuteScalar();
                    mySQLiteCommand.Parameters.Clear();
                    if (int.Parse(i.ToString().Trim()) > 0) continue;

                    mySQLiteCommand.CommandText = "INSERT INTO MelaMessagesClass(ClassName) VALUES(@MsgClass)";
                    mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
                    mySQLiteCommand.Parameters["@MsgClass"].Value = f;
                    mySQLiteCommand.ExecuteNonQuery();
                    Form1.WriteToDBInDisk(mySQLiteCommand);
                }
                //RefreshMsgClass();

                MessageBox.Show("更新完成！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                MySqlConnection.Close();
            }
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //使用Web进行发送
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages";
            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            if (mySQLiteDataReader.Read())
            {
                String MsgClass = mySQLiteDataReader["MsgClass"].ToString().Trim();
                String Messages = mySQLiteDataReader["Messages"].ToString().Trim();
                String result = Form1.PostMsgtoWeb(MsgClass, Messages);
            }
            mySQLiteDataReader.Close();
        }

        private void button27_Click(object sender, EventArgs e)
        {
            //更新配置
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "UPDATE MelaMessages SET IsEnabled=@IsEnabled,MsgClass=@MsgClass,Messages=@Messages WHERE ID=@ID";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@IsEnabled"));
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@MsgClass"));
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@Messages"));
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ID"));
            mySQLiteCommand.Parameters["@IsEnabled"].Value = this.checkBox5.Checked;
            mySQLiteCommand.Parameters["@MsgClass"].Value = this.comboBox2.Text;
            mySQLiteCommand.Parameters["@Messages"].Value = this.textBox12.Text;
            mySQLiteCommand.Parameters["@ID"].Value = UserID;
            mySQLiteCommand.ExecuteNonQuery();
            Form1.WriteToDBInDisk(mySQLiteCommand);

            //更新Class
            mySQLiteCommand.CommandText = "SELECT count(*) FROM MelaMessagesClass WHERE ClassName=@ClassName";
            mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ClassName"));
            mySQLiteCommand.Parameters["@ClassName"].Value = this.comboBox2.Text;
            object i = mySQLiteCommand.ExecuteScalar();
            mySQLiteCommand.Parameters.Clear();
            //没有，则插入
            if (int.Parse(i.ToString().Trim())==0)
            {
                mySQLiteCommand.CommandText = "INSERT INTO MelaMessagesClass(ClassName) VALUES(@ClassName)";
                mySQLiteCommand.Parameters.Add(new SQLiteParameter("@ClassName"));
                mySQLiteCommand.Parameters["@ClassName"].Value = this.comboBox2.Text;
                mySQLiteCommand.ExecuteNonQuery();
                Form1.WriteToDBInDisk(mySQLiteCommand);
                mySQLiteCommand.Parameters.Clear();
            }
            RefreshMessageList();
            MessageBox.Show("保存成功！");
        }

        private void listView5_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView5.SelectedIndices != null && listView5.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView5.SelectedIndices;
                UserID = listView5.Items[c[0]].SubItems[0].Text;
                SetListViewText(UserID);
            }
        }
        private void SetListViewText(String ID)
        {
            SQLiteCommand mySQLiteCommand = new SQLiteCommand("", Form1.memSQLiteConnection);
            mySQLiteCommand.CommandText = "SELECT * FROM MelaMessages WHERE ID=" + ID;
            SQLiteDataReader mySQLiteDataReader = mySQLiteCommand.ExecuteReader();
            if (mySQLiteDataReader.Read())
            {
                this.comboBox2.Text = mySQLiteDataReader["MsgClass"].ToString().Trim();
                this.textBox12.Text = mySQLiteDataReader["Messages"].ToString().Trim();
                Boolean IsEnabled = Boolean.Parse(mySQLiteDataReader["IsEnabled"].ToString().Trim());
                this.checkBox5.Checked = IsEnabled;
            }
            mySQLiteDataReader.Close();

        }

        private void FormMsgAdmin_Load(object sender, EventArgs e)
        {
            RefreshMessageList();
        }
    }
}
